package registration;
//This code is by Jonthan Axford
import java.sql.ResultSet;
import java.sql.SQLException;
import java.beans.*;
import java.io.Serializable;

import db.teDB;

public class register implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String fName;
	/**
	 * @param fName
	 * @param lName
	 * @param addr
	 * @param city
	 * @param postal
	 * @param country
	 * @param hPhone
	 * @param wPhone
	 * @param email
	 * @param passwd
	 */
	public register(String fName, String lName, String addr, String city, String prov,
			String postal, String country, String hPhone, String wPhone,
			String email, String passwd) {
		super();
		this.fName = fName;
		this.lName = lName;
		this.addr = addr;
		this.city = city;
		this.prov = prov;
		this.postal = postal;
		this.country = country;
		this.hPhone = hPhone;
		this.wPhone = wPhone;
		this.email = email;
		this.passwd = passwd;
		success=false;
		myID="";
	}
	public register()
	{
		this.fName = "";
		this.lName = "";
		this.addr = "";
		this.city = "";
		this.postal = "";
		this.country = "";
		this.hPhone = "";
		this.wPhone = "";
		this.email = "";
		this.passwd = "";
		success=false;
		myID="";
	}
	public String getfName() {
		return fName;
	}
	public void setfName(String fName) {
		this.fName = fName;
	}
	public String getlName() {
		return lName;
	}
	public void setlName(String lName) {
		this.lName = lName;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getPostal() {
		return postal;
	}
	public void setPostal(String postal) {
		this.postal = postal;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String gethPhone() {
		return hPhone;
	}
	public void sethPhone(String hPhone) {
		this.hPhone = hPhone;
	}
	public String getwPhone() {
		return wPhone;
	}
	public void setwPhone(String wPhone) {
		this.wPhone = wPhone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	public boolean isSuccessfull()
	{
		return success;
	}
	public boolean[] verifiedArray()
	{
		boolean verified[] = {true, true, true, true, true, true, true, true, true, true, true};
		if(lName.isEmpty() || !Val.Validate.genericText(lName))
			 verified[0]=false;
		if(fName.isEmpty() || !Val.Validate.genericText(fName))
			verified[1]=false;
		if(addr.isEmpty() || !Val.Validate.genericText(addr))
			verified[2]=false;
		if(city.isEmpty() || !Val.Validate.genericText(city))
			verified[3]=false;
		if(prov.isEmpty() || !Val.Validate.isValidISOCode(prov))
			verified[4]=false;
		if(postal.isEmpty() || !Val.Validate.genericText(postal))//isValidPostalZip
			verified[5]=false;
		if(country.isEmpty() || !Val.Validate.genericText(country))
			verified[6]=false;
		if(hPhone.isEmpty() || !Val.Validate.isValidPhoneFax(hPhone))
			verified[7]=false;
		if(wPhone.isEmpty() || !Val.Validate.isValidPhoneFax(wPhone))
			verified[8]=false;
		if(email.isEmpty() || !Val.Validate.isValidEmail(email))
			verified[9]=false;
		if(passwd.isEmpty() || !Val.Validate.genericText(passwd))
			verified[10]=false;
		return verified;
	}
	public boolean verify()
	{
		boolean verified[] = verifiedArray();
		for(int i=0; i<verified.length; i++)
			if(!verified[i])
				return false;
		return true;
	}
	public String getID()
	{
		return myID;
	}
	public String getProv() {
		return prov;
	}
	public void setProv(String prov) {
		this.prov = prov;
	}
	public boolean processReg()
	{
		if(verify())
		{
			try {
				teDB teCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
				ResultSet rs = teCon.getCustomRS("SELECT customerid FROM Customers WHERE upper(CustFirstName) = '"+fName.toUpperCase()+"' AND upper(CustLastName) = '"+lName.toUpperCase()+"'");
				if(rs.next())
					success=false;
				else
				{
					String UID = teCon.getGenKey("Customers", "CustomerId");
					myID=UID;
					String[] values = {myID, fName, lName, addr, city, prov, postal, country, hPhone, wPhone, email, "", "0"};
					if(teCon.insert("Customers", values))
					{
						String shortName = fName;
						if (fName.length()>4)
							shortName=fName.substring(0, 4);
						success=setLogin(UID, shortName+UID, passwd);
					}
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				success=false;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				success=false;
			}
		}
		return success;
	}
	public String[] getUser(String UID)
	{
		String[] toRet ={""};
		try {
			teDB dbCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			ResultSet rs = dbCon.getCustomRS("SELECT CustFirstName, CustLastName, CustAddress, CustCity, CustProv, CustPostal, CustCountry, CustHomePhone, CustBusPhone, CustEmail FROM Customers WHERE CustomerId = " + UID);
			rs.next();
			String[] ret ={rs.getString("CustFirstName"), rs.getString("CustLastName"), rs.getString("CustAddress"), rs.getString("CustCity"), rs.getString("CustProv"), rs.getString("CustPostal"), rs.getString("CustCountry"), rs.getString("CustHomePhone"), rs.getString("CustBusPhone"), rs.getString("CustEmail")};
			toRet=ret;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return toRet;
	}
	private boolean setLogin(String UID, String userName, String Pass)
	{
		try {
			teDB dbCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			String[] values = {UID, userName, Pass};
			dbCon.insert("CustomerLogin", values);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return false;
		}
		
		return true;
	}
	private boolean updateLogin(String UID, String userName, String Pass)
	{
		try {
			teDB dbCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			String[] values = {userName, Pass};
			String[] feilds = {"Username", "Password"};
			dbCon.update("CustomerLogin", feilds, values, "CustomerId = "+UID);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return false;
		}
		
		return true;
	}
	
	public String getLogin(int UID)
	{
		String ret ="";
		try {
			teDB dbCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			ResultSet rs = dbCon.getCustomRS("SELECT username FROM CustomerLogin where CustomerID = "+ Integer.toString(UID));
			rs.next();
			ret=rs.getString(1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			return "Error, please contact us";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return "No such user, please try again or contact us";
		}
		return ret;
	}
	public void UpdateReg(String ID)
	{
		try
		{
			myID=ID;
			teDB teCon = new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
			ResultSet rs = teCon.getCustomRS("SELECT * FROM Customers WHERE customerid = "+myID);
			if(rs.next())
			{
				String shortName = fName;
				if (fName.length()>4)
					shortName=fName.substring(0, 4);
				updateLogin(ID, shortName+ID, passwd);
				String[] fields = {"CustFirstName", "CustLastName", "CustAddress", "CustCity", "CustProv", "CustPostal", "CustCountry", "CustHomePhone", "CustBusPhone", "CustEmail"}, values={fName, lName, addr, city, prov, postal, country, hPhone, wPhone, email};
				teCon.update("customers", fields, values, "CustomerId = "+myID);
				success = true;
			}
		}
		catch (SQLException e) {
			// TODO: handle exception
			success=false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			success=false;
		}
		
	}
	private String myID;
	private String lName;
	private String addr;
	private String city;
	private String prov;
	private String postal;
	private String country;
	private String hPhone;
	private String wPhone;
	private String email;
	private String passwd;
	private boolean success;
}
